package ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.mainframeSub;

import java.util.ArrayList;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.model.History;

public class RFCDisplayTableHistory extends JTable
{
	private static final long serialVersionUID = 1L;

	private static final String[] TABLECOLUMNNAMES = { "ID", "Zeitstempel", "Benutzer", "Source", "Beschreibung" };
	
	@SuppressWarnings("unused")
	private int HISTORY_ID;
	private final int colID, colTimestmap, colUsr, colSrc, colDesc;
	private DefaultTableModel tableModel;

	private ArrayList<History> historyList;

	public RFCDisplayTableHistory()
	{
		this.colID = 25;
		this.colTimestmap = 140;
		this.colUsr = 140;
		this.colSrc = 40;
		this.colDesc = 10;
		this.historyList = new ArrayList<History>();
		this.setModel(this.setupTablemodel());
		this.setupColums();
	}

	public boolean isCellEditable(int x, int y)
	{
		return false;
	}

	private void setupColums()
	{
		this.getColumnModel().getColumn(0).setWidth(this.colID);
		this.getColumnModel().getColumn(0).setMaxWidth(this.colID+1);
		this.getColumnModel().getColumn(1).setMinWidth(this.colTimestmap);
		this.getColumnModel().getColumn(1).setMaxWidth(this.colTimestmap+1);
		this.getColumnModel().getColumn(2).setMinWidth(this.colUsr);
		this.getColumnModel().getColumn(2).setMaxWidth(this.colUsr+1);
		this.getColumnModel().getColumn(3).setMinWidth(this.colSrc);
		this.getColumnModel().getColumn(3).setMaxWidth(this.colSrc+1);
		this.getColumnModel().getColumn(4).setMinWidth(this.colDesc);
	}

	private TableModel setupTablemodel()
	{
		Object[][] data = null;
		this.tableModel = new DefaultTableModel(data, TABLECOLUMNNAMES);
		return tableModel;
	}

	public void clearTable()
	{
		while (this.tableModel.getRowCount() > 0)
		{
			for (int i = 0; i < this.tableModel.getRowCount(); i++)
			{
				this.tableModel.removeRow(i);
				if(!(this.historyList.size()==0))
				{					
					this.historyList.remove(i);
				}
			}
		}
	}

	public void addEntryToTable(History history)
	{
		this.historyList.add(history);
		String[] historyData = new String[5];
		historyData[0] = "" + History.getHISTORY_ID();
		historyData[1] = "" + history.getTimestamp();
		historyData[2] = "" + history.getUser();
		historyData[3] = "" + history.getSource();
		historyData[4] = "" + history.getDescription();
		this.tableModel.addRow(historyData);
		this.setEnabled(true);
	}
	
	public void displayNoEntry()
	{
		String[] historyData = new String[5];
		historyData[0] = "*";
		historyData[1] = "*";
		historyData[2] = "*";
		historyData[3] = "*";
		historyData[4] = "Kein Eintrag gefunden";
		this.tableModel.addRow(historyData);
		this.setEnabled(false);
	}

	public History getSelectedHistoryInTable()
	{
		if(this.getSelectedRow() >= 0 && this.historyList.size()>0)
		{
			return this.historyList.get(this.getSelectedRow());
		}
		else
		{
			return null;
		}
	}

	public History getElementatAtRow(int i)
	{
		return historyList.get(i);
	}
}